﻿using PLM.ConfigurationCenter.Model;
using PLM.ConfigurationCenter.SqlSugar;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PLM.ConfigurationCenter.CustomControl {
    public partial class ServerCard : UserControl {
        /// <summary>
        /// 
        /// </summary>
        public event Action<ServerModel> CradClick;

        public event Action<ServerModel> CradDoubleClick;

        public ServerModel CradModel;
        /// <summary>
        /// 
        /// </summary>
        public UIPanel uIPanel;
        /// <summary>
        /// 
        /// </summary>
        public UILight uiLight;
        /// <summary>
        /// 
        /// </summary>
        public ServerCard() {
            InitializeComponent();
            uIPanel = this.uiPanel1;
            uiLight = this.uiLight1;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        public void Init(ServerModel model) {
            CradModel = model;
            if (model.Status == 3) {
                this.uiLight.State = UILightState.On;
            } else if (model.Status == 4) {
                this.uiLight.State = UILightState.Blink;
            } else {
                this.uiLight.State = UILightState.Off;
            }
            this.uiMarkLabel1.Text = model.SiteLabel;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ServerCard_Click(object sender, EventArgs e) {
            var model = DbManager.DbContext.Queryable<ServerModel>().First(p => p.Id == CradModel.Id);
            Init(model);
            CradClick?.Invoke(CradModel);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ServerCard_DoubleClick(object sender, EventArgs e) {
            var model = DbManager.DbContext.Queryable<ServerModel>().First(p => p.Id == CradModel.Id);
            Init(model);
            CradDoubleClick?.Invoke(CradModel);
        }
    }
}
